﻿using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.Collections.Generic;
using Ops.Mpc.Parameters;

namespace Ops.Mpc.Wcf
{
    public class ServiceHeaderInjector : IClientMessageInspector 
    {

        #region IClientMessageInspector Members

        void IClientMessageInspector.AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
           
        }

        object IClientMessageInspector.BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            ServiceHeader serviceHeader = new ServiceHeader();

            foreach (KeyValuePair<string, string> keyValuePair in InitParameters.GetCurrent())
                serviceHeader.Data.Add(keyValuePair.Key, keyValuePair.Value);

            if(serviceHeader.Data.ContainsKey("ApplicationName"))
                serviceHeader.SourceApplication = serviceHeader.Data["ApplicationName"];

 
            request.Headers.Add(MessageHeader.CreateHeader("ServiceContext", "urn:Ops.Service:v1", serviceHeader));

            return null;
        }

        #endregion



    }
}
